package com.point.business.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.JsonObject;
import com.point.business.database.managers.UsersManager;
import com.point.business.util.UTFUtil;
import com.point.business.util.Role;

/**
 * Servlet implementation class LoginServlet
 */

public class EditUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EditUserServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		UTFUtil.utf8(request, response);
		PrintWriter out = response.getWriter();
		int id = Integer.parseInt(request.getParameter("id"));
		String fullName = request.getParameter("fullName");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String username = request.getParameter("username");
		String company = request.getParameter("company");
		String city = request.getParameter("city");
		String address = request.getParameter("address");
		String telephone = request.getParameter("telephone");
		String cellPhone = request.getParameter("cellPhone");
		int maxQueries = Integer.parseInt(request.getParameter("maxSearchQueries"));
		boolean canDoSL=false;
		if(request.getParameter("canDoSL") != null){
			canDoSL = true;
		}
		boolean canDoUOPB=false;
		if(request.getParameter("canDoUOPB") != null){
			canDoUOPB = true;
		}
		boolean canDoNO=false;
		if(request.getParameter("canDoNO") != null){
			canDoNO = true;
		}
		boolean canDoND=false;
		if(request.getParameter("canDoND") != null){
			canDoND = true;
		}
		//FIXME real values for: 
		//company,address,city,telephone,cellPhone
		boolean isUpdated = UsersManager.getInstance().updateUser(id, email,
				fullName, password, Role.USER, "", username, "", maxQueries, canDoNO, canDoND,
				canDoSL,canDoUOPB,company,address,city,telephone,cellPhone);
		JsonObject jsono = new JsonObject();
		if (isUpdated) {
			jsono.addProperty("success", true);
			jsono.addProperty("edit", true);
			out.print(jsono);
		} else {
			jsono.addProperty("errorMsg", "Some errors occured.");
			out.print(jsono);
		}
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}
}
